var express = require('express');
var router = express.Router();
const svgCaptcha = require('svg-captcha');

/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});

router.get("/captcha", function (req, res, next) {
  const captcha = svgCaptcha.create();
  res.set("X-Captcha", captcha.text.toLowerCase())
  res.type('svg');
  res.status(200).send(captcha.data);

})

router.post("/authcaptcha", function (req, res, next) {
  if (req.body.captcha === req.headers["x-captcha"]) {
    res.send({ errno: 200, message: "验证成功" })
  } else {
    const captcha = svgCaptcha.create();
    res.set("X-Captcha", captcha.text.toLowerCase())
    res.type('svg');
    res.send({errno:0,msg:"验证码错误，请重新填写",result:captcha.data})
  }
})
module.exports = router;
